﻿using HardcoreGame.Common.CustomSets;
using HardcoreGame.Common.Helpers;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using ReLogic.Content;
using System;
using System.IO;
using Terraria;
using Terraria.DataStructures;
using Terraria.ID;
using Terraria.ModLoader;

namespace HardcoreGame.Content.Items.Weapons.Sets.Acid
{
    public class Miasma : ModItem
    {
        public static Lazy<Asset<Texture2D>> glowmask;
        public override void Unload()
        {
            glowmask = null;
        }
        public Projectile spear;
        private static int swingDir = -1;
        private Vector2 directionToMouse;
        public override void SetStaticDefaults()
        {
            CustomItemSets.AcidWeapon[Type] = true;
            ItemID.Sets.ItemsThatAllowRepeatedRightClick[Type] = true;
        }
        public override void SetDefaults()
        {
            Item.width = Item.height = 50;
            Item.damage = 20;
            Item.knockBack = 5f;
            Item.useStyle = ItemUseStyleID.Shoot;
            Item.useTime = Item.useAnimation = 26;
            Item.DamageType = DamageClass.Melee;
            Item.shootSpeed = 10f;
            Item.shoot = ModContent.ProjectileType<Projectiles.Sets.Acid.Miasma>();
            Item.value = Item.sellPrice(0, 1, 2, 55);
            Item.rare = ItemRarityID.Blue;
            Item.noUseGraphic = Item.noMelee = true;
            Item.channel = true;
            Item.autoReuse = true;
            Item.crit = 4;
            if (!Main.dedServ)
            {
                glowmask = new Lazy<Asset<Texture2D>>(() => ModContent.Request<Texture2D>(Texture + "_Glow", (AssetRequestMode)2));
            }
        }
        public override void PostDrawInWorld(SpriteBatch spriteBatch, Color lightColor, Color alphaColor, float rotation, float scale, int whoAmI)
        {
            MyUtils.BasicInWorldGlowmask(Item, spriteBatch, (Texture2D)glowmask.Value, Color.White, rotation, scale);
        }
        public override bool CanUseItem(Player player)
        {
            return true;
        }
        public override bool AltFunctionUse(Player player)
        {
            return true;
        }
        public override void HoldItem(Player player)
        {
            if (player.altFunctionUse == 2)
            {
                if (player.itemTime == 0)
                {
                    return;
                }
                Vector2 shoulderPos = player.RotatedRelativePoint(player.MountedCenter, false, true) + new Vector2(-4 * player.direction, -2f);
                if (player.whoAmI == Main.myPlayer)
                {
                    directionToMouse = shoulderPos.DirectionTo(Main.MouseWorld);
                }
                float arc = 4.1908846f;
                float num = directionToMouse.ToRotation();
                float rot = MathF.Sin(MathF.Pow(player.itemAnimation / (float)player.itemAnimationMax, 5f) * 1.095f);
                float armRotation = num + swingDir * (arc * rot - arc * 0.5f);
                player.SetCompositeArmFront(true, 0, armRotation - 1.5707964f);
                if (spear != null)
                {
                    float armLen = 10f;
                    player.heldProj = spear.whoAmI;
                    if (rot < 0.45f)
                    {
                        float maxRot = 0.7853982f * Main.rand.NextFloat(0.1f, 0.225f);
                        float curRot = 0f;
                        Projectile s = spear;
                        s.velocity = directionToMouse.RotatedBy((double)(curRot - maxRot * 0.5f), default) * s.velocity.Length() * Main.rand.NextFloat(0.85f, 1.2f);
                        s.Center = shoulderPos + directionToMouse * armLen;
                        s.tileCollide = true;
                        spear = null;
                        return;
                    }
                    Projectile s2 = spear;
                    s2.Center = shoulderPos + armRotation.ToRotationVector2() * armLen;
                    s2.rotation = shoulderPos.DirectionTo(s2.Center).ToRotation();
                }
            }
        }
        public override void NetSend(BinaryWriter writer)
        {
            writer.WriteVector2(directionToMouse);
        }
        public override void NetReceive(BinaryReader reader)
        {
            directionToMouse = reader.ReadVector2();
        }
        public override bool Shoot(Player player, EntitySource_ItemUse_WithAmmo source, Vector2 position, Vector2 velocity, int type, int damage, float knockback)
        {
            if (CanUseItem(player))
            {
                if (player.altFunctionUse == 2)
                {
                    spear = new Projectile();
                    Projectile pr = Projectile.NewProjectileDirect(source, position, velocity, type, damage, knockback, player.whoAmI, 0f, 0f, 0f);
                    pr.ai[0] = 1f;
                    spear = pr;
                    swingDir = -swingDir;
                }
                else
                {
                    Projectile.NewProjectileDirect(source, player.MountedCenter, velocity, type, damage, knockback, player.whoAmI, 0f, 0f, 0f);
                }
            }
            return false;
        }
    }
}
